package com.atguigu.gulimall.member.interceptor;

import com.atguigu.common.vo.MemberResponseVo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * @author ly
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {

    public static ThreadLocal<MemberResponseVo> loginUser = new ThreadLocal<>();

    /**
     * 方法执行之前进行拦截未登录用户
     *
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        //请求处理之前获取请求的URI
//        String uri = request.getRequestURI();
//        //AntPathMatcher：spring提供的路径匹配器
//        boolean match = new AntPathMatcher().match("/member/**", uri);
//        //如果路径匹配成功（订单order），就放行
//        if (match) {
//            return true;
//        }
//        MemberResponseVo attribute = (MemberResponseVo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
//        if (attribute != null) {
//            //登录了
//            loginUser.set(attribute);
//            return true;
//        } else {
//            //没登录就重定向去登录页面
//            request.getSession().setAttribute("msg", "请先进行登录");
//            response.sendRedirect("http://auth.gulimall.com/login.html");
//            return false;
//        }
        return true;
    }
}
